package com.sias.spring.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-01 23:47:52
 */
@Aspect
@Component
public class LogAspect {

  private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);

  // @Pointcut("execution(* com.sias.controller..*(..))")
  @Pointcut("@annotation(com.sias.annotation.LoginRequired)")
  public void logPointcut(){

  }

  @Before("logPointcut()")
  public void before(JoinPoint joinPoint){
    logger.info("-------------");
    logger.info("调用"+joinPoint.getSignature());
    logger.info("-------------");
  }

  @AfterReturning(value = "logPointcut()",returning = "returnValue")
  public void after(JoinPoint joinPoint,Object returnValue){
    logger.info("-------------");
    logger.info("调用"+joinPoint.getSignature()+"返回值为："+returnValue);
    System.out.println(Arrays.toString(joinPoint.getArgs()));
    logger.info("-------------");
  }

}
